package com.blb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.entity.Driver;
import com.blb.entity.Logistics;
import com.blb.mapper.DriverMapper;
import com.blb.mapper.LogisticsMapper;
import com.blb.service.IDriverService;
import com.blb.service.ILogisticsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *物流管理的驾驶员管理
 * @author zyx
 * @since 2020-05-25
 */
@Controller
@RequestMapping("/driver")
public class DriverController {
    @Autowired
    private IDriverService iDriverService;

    @Resource
    DriverMapper driverMapper;

    @Resource
    LogisticsMapper logisticsMapper;

    //查看数据
    @RequiresPermissions("driver")
    @RequestMapping("/d_list")
    @ResponseBody
    public Page<Driver> list(int page, int limit,Model model) {
        return iDriverService.page(new Page<Driver>(page,limit));
    }

    //跳转添加页面
    @RequiresPermissions("driver:add")
    @RequestMapping("/toAdd")
    public String toAdd(Model model){
        List<Logistics> list = logisticsMapper.selectList(new QueryWrapper<Logistics>());
        model.addAttribute("list",list);
        return "logistics/driver/driver_add";
    }
    //跳转修改页面
    @RequiresPermissions("driver:update")
    @RequestMapping("/toUpdate")
    public String toUpdate(Model model){
        List<Logistics> list = logisticsMapper.selectList(new QueryWrapper<Logistics>());
        model.addAttribute("list",list);
        return "logistics/driver/driver_update";
    }


    //根据查询条件搜索
    @RequiresPermissions("driver")
    @ResponseBody
    @RequestMapping("/select")
    public Page<Driver> select(int page, int limit, String tdrivername){
        System.out.println(tdrivername);
        return driverMapper.selectPage(new Page<Driver>(page,limit),new QueryWrapper<Driver>().like("t_drivername",tdrivername));
    }

    //根据id删除
    @RequiresPermissions("driver:deleted")
    @RequestMapping("/d_delById")
    @ResponseBody
    public  boolean del(String id){
        return iDriverService.removeById(id);
    }

    //添加数据
    @RequiresPermissions("driver:add")
    @RequestMapping("/d_add")
    @ResponseBody
    public boolean add(Driver driver){
        String id = UUID.randomUUID().toString().replace("-","").substring(0,4);
        driver.setId(id);
        driver.setTDriverid(id);
        driver.setCreateTime(new Date());
        driver.setCompanyId("1");
        return iDriverService.save(driver);
    }


    //根据id修改数据
    @RequiresPermissions("driver:update")
    @RequestMapping("/d_update")
    @ResponseBody
    public boolean update(Driver driver){
        return iDriverService.update(driver,new UpdateWrapper<Driver>().eq("id",driver.getId()));

    }

    //查看身份证身份存在
    @RequiresPermissions("driver")
    @RequestMapping("/isIdno")
    @ResponseBody
    public boolean selByIden(String tDriveridno){
        boolean res = false;
        List<Driver> list = iDriverService.list(new QueryWrapper<Driver>().eq("t_driveridno",tDriveridno));
        if(list == null){
            System.out.println(list==null);
            res =true;
        }else {
            System.out.println(list==null);
        }
        return res;
    }



}

